package com.lanou.beautifulwardrobe.Space;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader;
import com.lanou.beautifulwardrobe.R;
import com.lanou.beautifulwardrobe.Space.Adapter.SpaceRecyclerViewAdapter;
import com.lanou.beautifulwardrobe.activity.LoginActivity;

/**
 * Created by dllo on 15/10/26.
 */
public class SpaceFragment extends Fragment implements View.OnClickListener {

    private RecyclerView spaceRecyclerView;
    private SpaceRecyclerViewAdapter adapter;
    private GridLayoutManager manager;
    private RecyclerViewHeader spaceHeader;
    private Button aloginBtn;

    private static SpaceFragment spaceFragment;


    public static SpaceFragment getspaceFragment() {
        if (spaceFragment == null) {
            spaceFragment = new SpaceFragment();
        }
        return spaceFragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.maintab_space_item, null);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        spaceRecyclerView = (RecyclerView) view.findViewById(R.id.spaceRecyclerView);
        spaceHeader = (RecyclerViewHeader) view.findViewById(R.id.spaceHeader);
        aloginBtn = (Button) view.findViewById(R.id.spaceLogin);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
        listener();
    }

    private void initView() {
        adapter = new SpaceRecyclerViewAdapter();
        manager = new GridLayoutManager(getActivity(), 1);
    }

    private void initData() {
        spaceRecyclerView.setLayoutManager(manager);
        spaceRecyclerView.setAdapter(adapter);
        spaceHeader.attachTo(spaceRecyclerView, true);
    }

    private void listener() {
        aloginBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(getActivity(), LoginActivity.class);
        startActivity(intent);
    }
}
